package com.qf.fmall2203.odder.controller;

import com.qf.fmall2203.odder.entity.ShoppingCart;
import com.qf.fmall2203.odder.service.IShoppingCartService;
import com.qf.fmall2203.vo.ResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 购物车  前端控制器
 * </p>
 *
 * @author ljc
 * @since 2023-02-16
 */
@RestController
@RequestMapping("/shopcart")
@Slf4j
@CrossOrigin
public class ShoppingCartController {

    @Autowired
    IShoppingCartService iShoppingCartService;

    @PostMapping("/add")
    public ResultVo add(@RequestBody ShoppingCart shoppingCart){
    log.debug("shoppingCart:{}",shoppingCart);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        String format = simpleDateFormat.format(new Date());

        shoppingCart.setCartTime(format);

        iShoppingCartService.save(shoppingCart);

        return ResultVo.ok();
    }

    @GetMapping("/list")
    public ResultVo list(Integer userId){
        log.debug("userId:{}",userId);

        return iShoppingCartService.listShopping(userId);

    }

    @PutMapping("update/{cart_id}/{cartNum}")
    public ResultVo updateNum(@PathVariable("cart_id") Integer cart_id,@PathVariable("cartNum")String cartNum){

        log.debug("cart_id:{},cartNum:{}",cart_id,cartNum);

        return iShoppingCartService.updateNum(cart_id,cartNum);
    }
    @GetMapping("/listbycids")
    public ResultVo listByCIds(String cids){

      return   iShoppingCartService.listByCIds(cids);
    };



}
